<?php
	class CDB
	{
		var $connected;
		var $link_host;
		var $link_db;
		var $last_q;
		var $tables;
		var $ntables;

		function CDB()
		{
			$this->connected = false;
			$this->link_host = null;
			$this->link_db = null;
			$this->last_q = null;
			$this->tables = null;
			$this->ntables = 0;
		}
		function __destruct()
		{
			$this->disconnect();
		}
		function disconnect()
		{
			if($this->connected)
			{
				mysql_close($this->link_host);
				$this->connected = false;
				$this->link_host = null;
				$this->link_db = null;
				$this->last_q = null;
				$this->tables = null;
				$this->ntables = 0;
			}
		}
		function connect()
		{
			if(!($this->link_host=mysql_connect($GLOBALS['db_host'],
				$GLOBALS['db_user'], $GLOBALS['db_pass'])))
				return false;
			if(!($this->link_db=mysql_select_db($GLOBALS['db_name'])))
				return false;
			$this->connected = true;
			return true;
		}
		function getTables()
		{
			if($this->tables!=null) return $this->tables;
			$this->tables = array();
			$this->query("SHOW TABLES");
			while($row=mysql_fetch_array($this->last_q))
			{
				$this->tables[$this->ntables++] = $row[0];
			}
			return $this->tables;
		}
		function query($q)
		{
			return ($this->last_q = mysql_query($q, $this->link_host));
		}
		function lastCount()
		{
			return mysql_num_rows($this->last_q);
		}

	}

?>